package com.qf;

import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
import java.io.IOException;

//小鸟类
public class Bird {
    public BufferedImage moveBird;
    public BufferedImage moveBirds[];
    public int x;
    public int y;
    public int width;
    public int height;
    public int index = 0;

    public double speed = 0; //初始上抛速度
    public double upSpeed = 15; //上抛速度
    public double t = 0.2;    //位移时间
    public double g = 4;    //重力加速度
    public double s = 0;      //经过t秒后小鸟的位移距离

    public Bird() {
        try {
            x = 120;
            y = 220;
            //读取项目中图片,赋值给属性:bg
            moveBird = ImageIO.read(getClass().getResource("image/0.png"));

            moveBirds = new BufferedImage[8];//创建长度为8的数组,保存小鸟0-7的图片
            for (int i = 0; i < moveBirds.length; i++) {
                moveBirds[i] = ImageIO.read(getClass().getResource("image/" + i + ".png"));
            }
            width = moveBird.getWidth();
            height = moveBird.getHeight();
        } catch (IOException e) {
            System.out.println("图片不存在");
        }
    }

    public void fly() {//小鸟飞翔的方法
        index++;
        moveBird = moveBirds[index / 10 % 8];
    }

    public void up() {//小鸟速度赋值
        speed = upSpeed;
    }

    public void down() {//小鸟移动效果
        double v = speed;
        s = v * t - g * t * t / 2; //小鸟上抛的距离减去向下自由落体的距离
        y = y - (int) s;//小鸟原y轴坐标-小鸟上升距离=小鸟新坐标
        speed = v - g * t;//小鸟上抛速度-小鸟自由落体速度=小鸟新速度
    }
}
